package com.naiterui.ehp.bs.patient.service.impl;

import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.patient.exception.ExceptionCodes;
import com.naiterui.ehp.bs.patient.service.ISendSMSService;
import com.naiterui.ehp.bs.patient.utils.cache.CommonCacheUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
 * @Description TODO
 * @Author gongbaoqiang
 * @Date 2020/7/10 11:03 AM
 */
@Slf4j
@Service
public class SendSMSServiceImpl implements ISendSMSService {

    @Override
    public void validateMsg(Long patientId, String phone, Integer actionType, String code) throws BusinessException {
        String cacheCode = CommonCacheUtil.getCodeFromRedis(actionType, phone);
        if (StringUtils.isBlank(cacheCode)) {
            log.error("验证码过期 : patientId {},phone {},actionType {}", patientId,
                    phone, actionType);
            throw new BusinessException(ExceptionCodes.PHONE_CODE_EXPIRE_PT);
        } else if (!cacheCode.equals(code)) {
            log.error("验证码错误: patientId {},phone {},actionType {}", patientId, phone,
                    actionType);
            throw new BusinessException(ExceptionCodes.PHONE_CODE_WRONG_PT);
        }
    }
}
